package cn.shyouchi.wuguanjia.android.ui.patrol.statistic;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.patrol.PatrolApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import cn.shyouchi.wuguanjia.android.widget.dialog.DatePickerDialogFragment;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PatrolTotalStatisticActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.rv_statistic)
    RecyclerView mRvStatistic;

    PatrolApi mPatrolApi;


    String projectid;

    PatrolMonthStatisticAdapter mPatrolMonthStatisticAdapter;

    List<PatrolStatisticEntity.ResultMessageBean> mPatrolStatisticEntities = new ArrayList<>();


    @Override
    public int initContentView() {
        return R.layout.activity_patrol_total_statistic;
    }

    @Override
    public void initInjector() {
        mPatrolApi = new PatrolApi(mContext);
        projectid = getIntent().getStringExtra("projectid");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        mPatrolMonthStatisticAdapter = new PatrolMonthStatisticAdapter(mPatrolStatisticEntities);

        mRvStatistic.setAdapter(mPatrolMonthStatisticAdapter);
        mRvStatistic.setLayoutManager(new LinearLayoutManager(mContext));


        mPatrolMonthStatisticAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.empty_view_layout, null));


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    public void showWhitDate(final int id) {
        final DatePickerDialogFragment datePickerDialogFragment = new DatePickerDialogFragment(getSupportFragmentManager(), "选择日期", "date", true);


        datePickerDialogFragment.setOnDateDissListener(new DatePickerDialogFragment.onTimePickedListener() {
            @Override
            public void onPicked(String date) {
                switch (id) {
                    case R.id.tv_start_time:
                        mTvStartTime.setText(date.substring(0, 7));
                        break;
                }
                fetchData(date);
            }


        });
        datePickerDialogFragment.show(getSupportFragmentManager(), "date");
    }


    private void fetchData(String date) {
        mPatrolApi.getMonthStatistic(projectid, date)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<PatrolStatisticEntity>(mContext) {

                    @Override
                    public void onStart() {
                        mPatrolStatisticEntities.clear();
                        super.onStart();
                    }

                    @Override
                    public void onNext(PatrolStatisticEntity entity) {

                        mPatrolStatisticEntities.addAll(entity.getResultMessage());

                    }


                    @Override
                    public void onError(Throwable e) {
                        mPatrolMonthStatisticAdapter.notifyDataSetChanged();
                        super.onError(e);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mPatrolMonthStatisticAdapter.notifyDataSetChanged();
                    }
                });


    }


    @OnClick(R.id.tv_start_time)
    public void onClick(View v) {
        showWhitDate(v.getId());
    }
}
